package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.NoticeListDto;
import com.hualude.roadsign.model.UserNoticeEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by pjn on 2017/3/3.
 */
public interface NoticeListRepository extends CrudRepository<UserNoticeEntity, Long> {

    public List<NoticeListDto> getNoticeList4DT(Integer start, Integer length, Integer userId, String search) throws Exception;

    public Integer getNoticeListCount4DT(String search, Integer userId) throws Exception;

    @Modifying
    @Query(value = "update UserNoticeEntity un set un.isRead=1,un.readTime=?2 where un.userNoticeId=?1")
    public Integer updateIsRead(Integer userNoticeId, String readTime) throws Exception;

    @Modifying
    @Query(value = "delete from UserNoticeEntity un where un.noticeId=?1")
    public Integer deleteNoticeUser(Integer noticeId) throws Exception;

    @Query(value = "select new com.hualude.roadsign.dto.NoticeListDto(un.userNoticeId,un.isRead,un.readTime,un.noticeId," +
            "n.noticeTitle,n.noticeContent,n.noticeSendTime) from UserNoticeEntity un,NoticeEntity n " +
            "where un.noticeId=n.noticeId and un.userId=?1 and n.noticeType=0 " +
            "order by un.isRead , n.noticeSendTime desc ")
    public List<NoticeListDto> getNoticeByUserId(Integer userId) throws Exception;

    @Modifying
    @Query(value = "delete from UserNoticeEntity un where un.userNoticeId=?1")
    public Integer deleteNotice(Integer userNoticeId) throws Exception;

    /**
     * @Description: 获取一条公告内容
     * @author chenzetong
     * @date 2017/4/15 19:20
     */
    @Query(value = "select new com.hualude.roadsign.dto.NoticeListDto(n.noticeId,n.createUserId,u.userName,n.noticeTitle" +
            ",n.noticeContent,n.noticeSendTime) from NoticeEntity n, UserEntity u " +
            "where n.noticeId=?1 and n.createUserId=u.userId ")
    public NoticeListDto getNoticeByNotceId(Integer noticeId) throws Exception;

    /**
     * @Description: APP获取公告
     * @Author: shao-yikun
     * @Date: 2017/5/8 0:58
     */
    public List<NoticeListDto> getNoticeByUserId4APP(Integer userId, Integer mCount, Integer mySize) throws Exception;

}
